package grg.learn.compose.network

import android.util.Log
import okhttp3.Interceptor
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

internal object AppRetrofit : RetrofitProvider {

    private lateinit var mRetrofit: Retrofit

    override fun retrofit(baseUrl: String): Retrofit {
        if (!::mRetrofit.isInitialized) {
            mRetrofit = retrofit {
                baseUrl(baseUrl)
                addConverterFactory(GsonConverterFactory.create())
                okhttpClient {
                    addInterceptor(HttpLoggingInterceptor(Logger).also {
                        it.level = HttpLoggingInterceptor.Level.BODY
                    })
                }
            }
        }
        return mRetrofit
    }

    private object Logger : HttpLoggingInterceptor.Logger {
        override fun log(message: String) {
            Log.i("=============", message)
            /**
             * create by iso88591 at 2021/9/1
             * todo
             */
        }
    }


}